<template>
        <el-card shadow="never" style="max-height: 70vh; overflow: auto">
            <el-form ref="form" :model="form" :rules="rules" label-width="90px" :disabled="disabled">
                <el-form-item label="id:" prop="id" v-show="false">
                    <el-input v-model="form.id" disabled />
                </el-form-item>
                <el-form-item label="封面:" prop="cover">
                    <ImageUpload v-model="form.cover" />
                </el-form-item>
                <el-row>
                    <el-col :span='12'>
                        <el-form-item label="所属租户:" prop="tenant_id">
                            <SelectPage v-model="form.tenant_id" :url="'/admin/px.tenant/selectPage'"
                                        placeholder="请选择所属租户"
                                        show_field="name" show_id="id" query_field="id" :multiple="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所属分类:" prop="category1">
                            <SelectCategory url="/admin/sq.category/tree" ref="category" :queryParams="{type: 4 }"
                                            :check-strictly="true"
                                            :value="[form.category1, form.category2, form.category3]"
                                            @change="onChangeCategory"></SelectCategory>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目标题:" prop="title">
                            <el-input v-model="form.title" placeholder="请输入项目标题" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="单价:" prop="price">
                            <el-input-number v-model="form.price" :step="0.01" :min="0"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="市场价:" prop="market_price">
                            <el-input-number v-model="form.market_price" :step="0.01" :min="0"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="库存数量:" prop="inventory_num">
                            <el-input-number v-model="form.inventory_num" :step="1" :min="0"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-if="disabled">
                        <el-form-item label="销售数量:" prop="sold_num">
                            <el-input-number v-model="form.sold_num" :step="1" :min="0"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态:" prop="status">
                            <el-select v-model="form.status" placeholder="请选择状态" clearable>
                                <el-option v-for="(item, index) in status" :key="index" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="审核状态:" prop="verify_status">
                            <el-select v-model="form.verify_status" placeholder="请选择审核状态" clearable>
                                <el-option v-for="(item, index) in verify_status" :key="index" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="审核失败原因:" prop="verify_content" v-if="form.verify_status === 3">
                            <el-input v-model="form.verify_content" placeholder="请输入审核失败原因" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目简介:" prop="description">
                            <el-input type="textarea" v-model="form.description" placeholder="请输入项目简介" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label='排序方式:' prop='sortweight'>
                            <el-input-number v-model='form.sortweight' :step='1' :min='0'></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="项目介绍:" prop="content">
                    <editor v-model="form.content" placeholder="请输入项目介绍" />
                </el-form-item>
                <el-form-item label="收藏数:" prop="favor_num" v-if="disabled">
                    <el-input-number v-model="form.favor_num" :step="1" :min="0"></el-input-number>
                </el-form-item>
                <el-form-item label="审核时间:" prop="verify_time" v-if="disabled">
                    <el-date-picker v-model="form.verify_time" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择审核时间" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm">确认 </el-button>
                    <el-button type="warning" @click="cancel">取消 </el-button>
                </el-form-item>
            </el-form>
        </el-card>
</template>

<script>
import { regionData } from 'element-china-area-data'
export default {
    name: 'EditForm',
    props: {
        findFormData: {
            type: Object,
            default: {}
        },
        editFormParams: {
            type: Object,
            default: {}
        },
        // 表单是否禁用
        disabled: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            // 编辑表单验证规则
            rules: {
                tenant_id: [{ required: true, message: "请输入所属租户", trigger: "blur" }],
                category1: [{ required: true, message: "请选择所属分类", trigger: "blur" }],
                cover: [{ required: true, message: "请上传封面", trigger: "blur" }],
                price: [{ required: true, message: "请输入单价", trigger: "blur" }],
                inventory_num: [{ required: true, message: "请输入库存数量", trigger: "blur" }],
                sold_num: [{ required: true, message: "请输入销售数量", trigger: "blur" }],
                status: [{ required: true, message: "请输入状态", trigger: "blur" }],
                favor_num: [{ required: true, message: "请输入收藏数", trigger: "blur" }],
                verify_status: [{ required: true, message: "请输入审核状态", trigger: "blur" }],
            },
            form: {  },
            // 区域选择配置
            areaOptions: regionData,
            status:[
               { label: "启用", value: 1 },
               { label: "禁用", value: 2 },
            ],
            verify_status:[
               { label: "审核中", value: 1 },
               { label: "审核通过", value: 2 },
               { label: "审核失败", value: 3 },
            ],

        }
    },
    watch: {
        findFormData: {
           handler(nval) {
                if (JSON.stringify(nval) != '{}') {
                    this.$nextTick(() => {
                        this.form = nval
                    })
                }
            },
            immediate: true,
            deep: false,
        },
    },
    methods: {
        // 取消按钮
        cancel() {
            this.$emit('editFormCancel')
        },
        submitForm() {
            this.$refs['form'].validate((valid, obj) => {
                if (valid) {
                    this.$emit('submitForm', this.form)
                }
            })
        },
        // 表单重置
        reset() {
            this.resetForm('form')
        },
        onChangeCategory(e, pathLabels) {
            e = e || []
            pathLabels = pathLabels || []
            this.$set(this.form, 'category3', e.length >= 3 ? e[2] : '')
            this.$set(this.form, 'category2', e.length >= 2 ? e[1] : '')
            this.$set(this.form, 'category1', e.length >= 1 ? e[0] : '')
        },
    }
}
</script>
